package cn.hebutrun.oulabot.common.util;

import cn.hebutrun.oulabot.entity.response.tianxing.TianxingGoodMorningResponse;
import cn.hebutrun.oulabot.entity.response.tianxing.TianxingMotivationalResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author zhaolong
 * @since 2022-10-09
 */
@Component
public class ApiUtil {

  @Value("${tianxing.api-key}")
  private String tianxingApiKey;

  public final String goodMorning() {
    String responseStr =
        HttpUtil.get("http://api.tianapi.com/zaoan/index", Map.of("key", tianxingApiKey));
    return JSONUtil.toBean(responseStr, TianxingGoodMorningResponse.class)
        .getNewslist()
        .get(0)
        .getContent();
  }

  public final String goodAfternoon() {
    return this.goodMorning().replaceAll("早安", "午安");
  }

  public final String goodNight() {
    String responseStr =
        HttpUtil.get("http://api.tianapi.com/wanan/index", Map.of("key", tianxingApiKey));
    return JSONUtil.toBean(responseStr, TianxingGoodMorningResponse.class)
        .getNewslist()
        .get(0)
        .getContent();
  }

  public String motivational() {
    String resStr =
        HttpUtil.get("http://api.tianapi.com/lzmy/index", Map.of("key", tianxingApiKey));
    TianxingMotivationalResponse.New aNew =
        JSONUtil.toBean(resStr, TianxingMotivationalResponse.class).getNewslist().get(0);
    return aNew.getSaying() + "--" + aNew.getSource() + "\n" + aNew.getTransl();
  }
}
